package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// ActivityOrder 活动订单
type ActivityOrder struct {
	ID              string  `gorm:"primaryKey;column:id;type:uint;size:64;comment:唯一ID" json:"id"`
	ActivityType    int     `gorm:"column:activity_type;type:int;size:8;comment:活动类型" json:"activity_type"`
	ActivityID      string  `gorm:"column:activity_id;type:string;size:30;index:idx_ActivityID_createdAt,priority:1;comment:活动id" json:"activity_id"`
	OutTradeNo      string  `gorm:"column:out_trade_no;type:string;size:30;comment:交易号" json:"out_trade_no"`
	ChildOutTradeNO string  `gorm:"column:child_out_trade_no;type:string;size:30;comment:子交易号" json:"child_out_trade_no"`
	RobotID         string  `gorm:"column:robot_id;type:string;size:30" json:"robot_id"`
	MerchantID      string  `gorm:"column:merchant_id;type:string;size:30" json:"merchant_id"`
	LocationID      string  `gorm:"column:location_id;type:string;size:30" json:"location_id"`
	ProvinceID      string  `gorm:"column:province_id;type:string;size:30;comment:省份id" json:"province_id"`
	CityID          string  `gorm:"column:city_id;type:string;size:30;comment:城市id" json:"city_id"`
	DistrictID      string  `gorm:"column:district_id;type:string;size:30;区县id" json:"district_id"`
	GoodsID         string  `gorm:"column:goods_id;type:string;size:30;comment:商品id" json:"goods_id"`
	GoodsPrice      float64 `gorm:"column:goods_price;type:decimal(10,2);comment:商品价格" json:"goods_price"`
	PaymentAmount   float64 `gorm:"column:payment_amount;type:decimal(10,2);comment:实付金额" json:"payment_amount"`
	// 优惠金额,注意：针对含有子单的订单，这个优惠金额是总的优惠金额，会在子单上冗余存储
	DiscountAmount float64 `gorm:"column:discount_amount;type:decimal(10,2);comment:优惠金额" json:"discount_amount"`
	PaymentType    int     `gorm:"column:payment_type;type:int;size:8;comment:支付方式" json:"payment_type"`
	OrderStatus    int     `gorm:"column:order_status;type:int;size:8;订单状态" json:"order_status"`
	CreatedAt      int64   `gorm:"column:created_at;autoCreateTime;type:int;size:64;index:idx_ActivityID_createdAt,priority:2;comment:创建时间" json:"created_at"`
}

// TableName 表名称
func (*ActivityOrder) TableName() string {
	return "activity_order"
}

// BeforeCreate 自增id
func (a *ActivityOrder) BeforeCreate(tx *gorm.DB) (err error) {
	if a.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	a.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &ActivityOrder{})
}
